﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Plate
{
    class CoCarImages
    {
        System.Data.SqlClient.SqlConnection Connection = new System.Data.SqlClient.SqlConnection(global::Plate.Properties.Settings.Default.crowdConnectionString);
        public void InsertFile(string filename)
        {
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(
                "INSERT INTO CarImages (Filename) VALUES(@Filename)",
                Connection);
            cmd.Parameters.Add("@Filename", System.Data.SqlDbType.NChar).Value = filename;
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            cmd.Connection.Close();

        }

        public void SaveImageAttributes(string FileName, ImageAttributes ImageAttributes)
        {
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(
                "UPDATE CarImages SET Attributes=@Attributes WHERE Filename=@Filename",
                Connection);
            cmd.Parameters.Add("@Filename", System.Data.SqlDbType.NChar).Value = FileName;
            cmd.Parameters.Add("@Attributes", System.Data.SqlDbType.NChar).Value = ImageAttributes.ToXml();
            cmd.Connection.Open();
            int result = cmd.ExecuteNonQuery();
            cmd.Connection.Close();
        }

        public ImageAttributes ReadImageAttributes(string filename)
        {
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(
                "SELECT Attributes FROM CarImages WHERE (Filename=@Filename)",
                Connection);
            System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter();
            cmd.Parameters.Add("@Filename", System.Data.SqlDbType.NChar, 255).Value = filename;
            adapter.SelectCommand = cmd;
            System.Data.DataTable table = new System.Data.DataTable();
            int result = adapter.Fill(table);

            if (table.Rows[0][0] is DBNull)
                return null;
            else
            {
                ImageAttributes attributes = ImageAttributes.FromXml((string)table.Rows[0][0]);
                return attributes;
            }
        }

        public void SafaTestResult(string filename, bool testresult)
        {
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(
                "UPDATE CarImages SET TestResult=@TestResult WHERE Filename=@Filename",
                Connection);
            cmd.Parameters.Add("@Filename", System.Data.SqlDbType.NChar).Value = filename;
            cmd.Parameters.Add("@TestResult", System.Data.SqlDbType.Bit).Value = testresult;
            cmd.Connection.Open();
            int result = cmd.ExecuteNonQuery();
            cmd.Connection.Close();
        }
        public void SafaNumberResult(string filename, bool numberresult)
        {
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(
                "UPDATE CarImages SET NumberResult=@NumberResult WHERE Filename=@Filename",
                Connection);
//          cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add("@Filename", System.Data.SqlDbType.NChar).Value = filename;
            cmd.Parameters.Add("@NumberResult", System.Data.SqlDbType.Bit).Value = numberresult;
            cmd.Connection.Open();
            int result = cmd.ExecuteNonQuery();
            cmd.Connection.Close();
        }
        public void SafaCarNumber( string filename, string carnumber)
        {
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(
                "UPDATE CarImages SET CarNumber=@CarNumber WHERE Filename=@Filename",
                Connection);
            cmd.Parameters.Add("@Filename", System.Data.SqlDbType.NChar).Value = filename;
            cmd.Parameters.Add("@CarNumber", System.Data.SqlDbType.VarChar).Value = carnumber;
            cmd.Connection.Open();
            int result = cmd.ExecuteNonQuery();
            cmd.Connection.Close();
        }
    }
}
